package controller;


import model.Manager;
import service.ManagerService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

@Controller
public class ManagerContoller{
    private ManagerService managerService;

    public void setManagerService(ManagerService managerService){
        this.managerService=managerService;
    }

    @RequestMapping("/getAll_manager")
    public ModelAndView getAll() throws Exception{
        ArrayList<Manager> manager=managerService.getAllManagers();
        ModelAndView modelAndView=new ModelAndView("show_manager");
        modelAndView.addObject("managers",manager);
        return modelAndView;
    }

    @RequestMapping("/delete_manager")
    public String delete(HttpServletRequest request, HttpServletResponse response,
                         RedirectAttributes redirectAttributes) throws Exception{
        int ano= Integer.parseInt(request.getParameter("ano"));
        boolean success=managerService.deleteManager(ano);

        if(success){
            redirectAttributes.addFlashAttribute("succ", "删除成功！");
            return "redirect:/show_manager.jsp";
        }else {
            redirectAttributes.addFlashAttribute("error", "删除失败！");
            return "redirect:/show_manager.jsp";
        }

    }

    @RequestMapping("/get_manager")
    public ModelAndView get(HttpServletRequest request,HttpServletResponse response) throws Exception{
        int ano= Integer.parseInt(request.getParameter("ano"));
        ArrayList<Manager> manager=managerService.getManager(ano);
        ModelAndView modelAndView=new ModelAndView("show_manager1");
        modelAndView.addObject("manager",manager);
        return modelAndView;
    }

    @RequestMapping("/edit_manager")
    public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
        int ano= Integer.parseInt(request.getParameter("ano"));
        ArrayList<Manager> manager=managerService.getManager(ano);
        ModelAndView modelAndView=new ModelAndView("show_manager");
        modelAndView.addObject("manager",manager);
        return modelAndView;
    }

    @RequestMapping("/doEdit_manager")
    public String doEdit(HttpServletRequest request,HttpServletResponse response,
                         String aPassword,String aName,String sex,String role,String position,String telephone,
                         RedirectAttributes redirectAttributes)throws Exception{
        String ano= request.getParameter("ano");
        Manager manager=new Manager();
        manager.setAno(ano);
        manager.setaPassword(aPassword);
        manager.setaName(aName);
        manager.setSex(sex);
        manager.setRole(role);
        manager.setPosition(position);
        manager.setTelephone(telephone);
        boolean succ=managerService.editManager(manager);
        if(succ){
            redirectAttributes.addFlashAttribute("succ", "修改成功！");
            return "redirect:/show_manager.jsp";
        }else {
            redirectAttributes.addFlashAttribute("error", "修改失败！");
            return "redirect:/show_manager.jsp";
        }

    }

    @RequestMapping("/add_manager")
    public ModelAndView add()throws Exception{
        ModelAndView modelAndView=new ModelAndView("show_manager");
        return modelAndView;

    }

    @RequestMapping("/doAdd_manager")
    public String doAdd(String ano,String aPassword,String aName,String sex,
                        String role,String position,String telephone,RedirectAttributes redirectAttributes)throws Exception{
        Manager manager=new Manager();
        manager.setAno(ano);
        manager.setaPassword(aPassword);
        manager.setaName(aName);
        manager.setSex(sex);
        manager.setRole(role);
        manager.setPosition(position);
        manager.setTelephone(telephone);
        boolean succ=managerService.addManager(manager);
        if (succ){
            redirectAttributes.addFlashAttribute("succ", "添加成功！");
            return "redirect:/show_manager.jsp";
        }else {
            redirectAttributes.addFlashAttribute("succ", "添加失败！");
            return "redirect:/show_manager.jsp";
        }
    }
}
